using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Formboard.Resources;
using System.Threading;

namespace Formboard
{
    public partial class Form1 : Form
    {
        Login loginMenu;
        AccountCreation creationMenu;
        Menu gameLauncherMenu;
        public UserControlBoard gameBoard;
        SideMenu ingameMenu;
        public HttpRequestProvider requester;
        public String myscreenname;
        public Board CurrentBoard;
        public Thread threadupdate;
        public bool requesterChecked;
        public Form1()
        {
            requesterChecked = false;
            InitializeComponent();
            threadupdate = new Thread(new ThreadStart(checkForUpdates));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.loginMenu = new Login();
            this.Controls.Add(loginMenu);
        }

        public void goto_gameLaucher_from_login()
        {
            this.Controls.Remove(loginMenu);
            gameLauncherMenu = new Menu();
            this.Controls.Add(gameLauncherMenu);
        }
        public void goto_gameLaucher_from_board()
        {
            this.Controls.Remove(gameBoard);
            this.Controls.Remove(ingameMenu);
            this.Controls.Add(gameLauncherMenu);
        }
        public void goto_login_from_gamelauncher()
        {
            this.Controls.Remove(gameLauncherMenu);
            this.Controls.Add(loginMenu);
        }
        public void goto_login_from_accountcreation()
        {
            this.Controls.Remove(creationMenu);
            this.Controls.Add(loginMenu);
        }
        public void goto_board(Board board)
        {
            this.Controls.Remove(gameLauncherMenu);
            CurrentBoard = board;
            gameBoard = new UserControlBoard(board,myscreenname);
            
            this.Controls.Add(gameBoard);
            ingameMenu = new SideMenu(myscreenname,gameBoard.myColor);
            this.Controls.Add(ingameMenu);
            if (board.moves.Count() > 0)
            {
                bool blackPlaying = true;
                for (int i = 0; i < board.moves.Count(); i++)
                {
                    if (blackPlaying)
                    {
                        updateHistory(i + 1, EnumCell.Black, (board.moves[i].Row) + 1, (board.moves[i].Column) + 1);
                        blackPlaying = false;
                    }
                    else
                    {
                        updateHistory(i + 1, EnumCell.White, (board.moves[i].Row) + 1, (board.moves[i].Column) + 1);
                        blackPlaying = true;
                    }
                }
            }
            threadupdate.Start();
            ingameMenu.Location = new Point(602, 0);
        }
        public void goto_accountcreation()
        {
            this.Controls.Remove(loginMenu);
            creationMenu = new AccountCreation();
            this.Controls.Add(creationMenu);
        }

        public void checkForUpdates()
        {
            while (true)
            {
                Move newMove = requester.getLastSequence(CurrentBoard);
                if (newMove != null)
                {
                    if (newMove.Sequence > gameBoard.lastSeqNumber && !gameBoard.myturn)
                    {
                        if (newMove.Row == -1)
                        {
                            updateHistory(newMove.Sequence, gameBoard.enemyColor, newMove.Row, newMove.Column);
                            gameBoard.lastSeqNumber++;
                            gameBoard.myturn = true;
                        }
                        else
                        {
                            updateHistory(newMove.Sequence, gameBoard.enemyColor, (newMove.Row) + 1, (newMove.Column) + 1);
                            CalculMove nextMove = new CalculMove(gameBoard.goban, gameBoard.enemyColor, newMove.Row + 1, newMove.Column + 1);
                            gameBoard.lastSeqNumber++;
                            gameBoard.update_graphic_board();
                            gameBoard.myturn = true;
                        }
                    }
                    
                }
                Thread.Sleep(5000);
            }
        }

        public void updateHistory(int sequ, EnumCell color, int x, int y)
        {
            if (x == -1)
            {
                String move = sequ.ToString() + " - " + color.ToString() + " - Turn skipped";
                ingameMenu.listBoxHistory.Items.Add(move);
            }
            else
            {
                String move = sequ.ToString() + " - " + color.ToString() + " - x: " + x.ToString() + " y: " + y.ToString();
                ingameMenu.listBoxHistory.Items.Add(move);
            }
            
        }
    }
}
